package com.brkj.footprint;

import java.io.File;
import java.io.FileNotFoundException;

import com.brkj.main.MyInfo;
import com.brkj.util.*;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import com.brkj.httpInterface.BaseAjaxParams;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.brkj.main.R;
import com.brkj.httpInterface.HttpInterface.HIF_SetPersnalInfo;
import com.brkj.util.view.BaseActivity;

public class SettingActivity extends BaseActivity{

	@ViewInject(id=R.id.userImg) ImageView userImg;
	@ViewInject(id=R.id.changeHeadImg,click="click_changeHeadImg") View changeHeadImg;
	@ViewInject(id=R.id.gender_men) RadioButton gender_men;
	@ViewInject(id=R.id.gender_women) RadioButton gender_women;
	@ViewInject(id=R.id.name) EditText name;
	@ViewInject(id=R.id.department) EditText department;
	@ViewInject(id=R.id.post) EditText post;
	@ViewInject(id=R.id.submit,click="click_submit") Button submit;
	private PickPhoto pickPhoto;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setAnimType(BaseActivity.RIGHT_ENTER_LEFT_EXT);
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.personal_info_set);
		setActivityTitle("设置");
		setReturnBtn();
		
		initData();
		
		pickPhoto = new PickPhoto(this, new PickPhoto.PickResult() {
			
			@Override
			public void doWhat(Bitmap photo) {
				userImg.setImageBitmap(photo);
			}
		});
		
		pickPhoto.setImgSavePath(ConstAnts.IMG_CATCH_DIR);
		pickPhoto.setPickParam(1, 1, 100, 100);
	}

	private void initData() {
		if(MyApplication.myInfo.getMyHeadImg().exists()){
			userImg.setBackgroundDrawable(BitmapUtil.FiletoDrawable(MyApplication.myInfo.getMyHeadImgPath()));
		}else{
			ImgShow.displayHead(userImg, MyApplication.myInfo.getHeadUrl());
		}
		
		if(MyApplication.myInfo.getGender().equals("女")){
			gender_women.setChecked(true);
			gender_men.setChecked(false);
		}else{
			gender_women.setChecked(false);
			gender_men.setChecked(true);
		}
		
		name.setText(MyApplication.myInfo.getName());
		department.setText(MyApplication.myInfo.getDepartment());
		post.setText(MyApplication.myInfo.getPost());
		
		
	}

	public void click_changeHeadImg(View v){
		
		pickPhoto.pick();
	}
	
	public void click_submit(View v){
		BaseAjaxParams params = new BaseAjaxParams();
		params.put(HIF_SetPersnalInfo.params.userID, MyApplication.myUserID);
		params.put(HIF_SetPersnalInfo.params.name, name.getText().toString());
		if(gender_women.isChecked()){
			params.put(HIF_SetPersnalInfo.params.gender, "女");
		}else{
			params.put(HIF_SetPersnalInfo.params.gender, "男");
		}
		params.put(HIF_SetPersnalInfo.params.post, post.getText().toString());
		params.put(HIF_SetPersnalInfo.params.department, department.getText().toString());
		
		
		File imageFile = pickPhoto.GetImageFile();
		if(imageFile != null){
			try {
				params.put(HIF_SetPersnalInfo.params.headUrl, pickPhoto.GetImageFile());
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		FinalHttp fh = new FinalHttp();
		
		fh.post(HIF_SetPersnalInfo.address, params, new MyAjaxCallBack<Object>(this) {

			
			@Override
			public void onFailure(Throwable t, String strMsg) {
				// TODO Auto-generated method stub
				super.onFailure(t, strMsg);
				showToast("保存失败");
			}

			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				
				HIF_SetPersnalInfo.result result = 
						JsonUtil.fromJson((String)t, HIF_SetPersnalInfo.result.class);
				
				if(result != null){
					if(result.result == 1){
						showToast("保存成功！");

                        MyInfo myInfo = new MyInfo(SettingActivity.this);
                        File file = myInfo.getMyHeadImg();
                        if(file.exists()){
                            file.delete();
                        }
						MyApplication.myInfo.getInfoFromNet();

					}else{
						showToast("保存失败：" + result.reason);
					}
					
					return;
				}else{
					showToast("保存失败");
				}
			}
			
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		pickPhoto.onActivityResult(requestCode, resultCode, data);
	}
	
	@Override
	public void finish() {
		setResult(RESULT_OK);
		super.finish();
	}
}
